package com.itzyf.pokemondata.bean

import android.util.Log

/**
 * @author 依风听雨
 * @version 创建时间：2018/12/29 14:34
 */
data class PokemonDo(
    var hp: Int = 0,
    var pa: Int = 0,
    var pd: Int = 0,
    var sa: Int = 0,
    var sd: Int = 0,
    var speed: Int = 0,
    var skin: String = "",
    var specialTraining: String? = null,
    var st2: String? = null,
    var isStar: Boolean = false,
    var red1: String = "",
    var skinImg: String = ""
) {
    companion object {
        //努力值
        private const val EFFORT = 2028
        //血量
        const val HP = 0x000001
        //物攻
        const val PA = 0x000010
        //物防
        const val PD = 0x000100
        //特攻
        const val SA = 0x001000
        //特防
        const val SD = 0x010000
        //速度
        const val SPEED = 0x100000
    }

    private var baseHp: Int = 0
    private var basePa: Int = 0
    private var basePd: Int = 0
    private var baseSa: Int = 0
    private var baseSd: Int = 0
    private var baseSpeed: Int = 0

    private var contract = 0
    private var characterRise = 0
    private var characterDecline = 0
    private var effort = 0
    private var guildAttackValue = 0.0
    private var guildDefenseValue = 0.0
    private var isIntimacy = false
    private var isSkin = false
    var isMega = true

    //是否有道具加成
    var isProp = false
    //加成的结果
    var addition: String = ""
    //活力特性
    private var isHustle = false
    //一猩一意
    private var isTactics=false
    //是否有特训
    private var specialTrainingIndex = 0
    private var isRed1 = false

    var img = ""
    var isBig = false
    /**
     * 契约值 直接增加
     */
    fun contract(contract: Int) {
        this.contract = contract
        updatePanel()
    }

    /**
     * 努力值处理
     */
    fun effort(effort: Int) {
        this.effort = effort
        updatePanel()
    }

    /**
     * 性格加成
     */
    fun character(characterRise: Int, characterDecline: Int) {
        this.characterRise = characterRise
        this.characterDecline = characterDecline
        updatePanel()
    }

    private fun Double.toRoundInt(): Int {
        return Math.round(this).toInt()
    }

    /**
     * 亲密度
     */
    fun intimacy(isIntimacy: Boolean) {
        this.isIntimacy = isIntimacy
        updatePanel()
    }

    /**
     * 皮肤
     */
    fun skin(isSkin: Boolean) {
        val split = getSkinValue()
        if (split.size == 6) {
            this.isSkin = isSkin
        }
        updatePanel()
    }

    /**
     * 道具加成
     */
    fun propAddition(addition: String) {
        this.addition = addition
        isProp = ("0,0,0,0,0,0" != addition)
        updatePanel()
    }

    fun getSkinValue(): List<String> {
        return skin.split(",")
    }

    fun isSkin(): Boolean {
        return skin.isNotEmpty() && skin.split(",").size == 6
    }

    fun setSpecialTrainingIndex(specialTrainingIndex: Int) {
        this.specialTrainingIndex = specialTrainingIndex
        updatePanel()
    }

    fun isRed1(isRed1: Boolean) {
        this.isRed1 = isRed1
        updatePanel()
    }

    //是否极巨化
    fun isBig(isBig: Boolean) {
        this.isBig = isBig
        updatePanel()
    }

    /**
     * 公会加成修正
     */
    fun guild(guildAttackValue: Double, guildDefenseValue: Double) {
        this.guildAttackValue = guildAttackValue
        this.guildDefenseValue = guildDefenseValue
        updatePanel()
    }

    /**
     * 更新面板
     */
    private fun updatePanel() {
        //先重置数据
        hp = baseHp
        pa = basePa
        pd = basePd
        sa = baseSa
        sd = baseSd
        speed = baseSpeed


        if (isRed1 && red1.isNotEmpty()) {
            val split = red1.split(",")
            if (split.size == 6) {
                hp += split[0].toInt()
                pa += split[1].toInt()
                pd += split[2].toInt()
                sa += split[3].toInt()
                sd += split[4].toInt()
                speed += split[5].toInt()
            }
        }

        //默认有特训，是否被关闭
        if (specialTraining != null) {
            //无特训
            if (specialTrainingIndex == 0) {
                val split = specialTraining!!.split(",")
                if (split.size == 6) {
                    hp -= split[0].toInt()
                    pa -= split[1].toInt()
                    pd -= split[2].toInt()
                    sa -= split[3].toInt()
                    sd -= split[4].toInt()
                    speed -= split[5].toInt()
                }
            } else if (specialTrainingIndex == 2) { //特训2
                val split2 = st2!!.split(",")
                if (split2.size == 6) {
                    hp += split2[0].toInt()
                    pa += split2[1].toInt()
                    pd += split2[2].toInt()
                    sa += split2[3].toInt()
                    sd += split2[4].toInt()
                    speed += split2[5].toInt()
                }
            }
            //index 1为特训1
        }

        val guildMega = if (isMega) 0.55 else 1.0
//        Log.i("PokemonDo","$guildMega")
        //公会--双攻
        if (guildAttackValue > 0) {
            pa += (pa * guildMega * guildAttackValue).toRoundInt()
            sa += (sa * guildMega * guildAttackValue).toRoundInt()
        }
        //公会--双防
        if (guildDefenseValue > 0) {
            pd += (pd * guildMega * guildDefenseValue).toRoundInt()
            sd += (sd * guildMega * guildDefenseValue).toRoundInt()
        }


        //性格++
        when (characterRise) {
            PA -> pa = (pa * 1.1).toRoundInt()
            PD -> pd = (pd * 1.1).toRoundInt()
            SA -> sa = (sa * 1.1).toRoundInt()
            SD -> sd = (sd * 1.1).toRoundInt()
            SPEED -> speed = (speed * 1.1).toRoundInt()
        }
        //性格--
        when (characterDecline) {
            PA -> pa = (pa * 0.9).toRoundInt()
            PD -> pd = (pd * 0.9).toRoundInt()
            SA -> sa = (sa * 0.9).toRoundInt()
            SD -> sd = (sd * 0.9).toRoundInt()
            SPEED -> speed = (speed * 0.9).toRoundInt()
        }

        //道具
        if (isProp) {
            //重新计算
            val prop = addition.split(",")
            if (prop[0].toInt() != 0)
                hp += (hp * prop[0].toInt() * 0.01).toRoundInt()
            if (prop[1].toInt() != 0)
                pa += (pa * prop[1].toInt() * 0.01).toRoundInt()
            if (prop[2].toInt() != 0)
                pd += (pd * prop[2].toInt() * 0.01).toRoundInt()
            if (prop[3].toInt() != 0)
                sa += (sa * prop[3].toInt() * 0.01).toRoundInt()
            if (prop[4].toInt() != 0)
                sd += (sd * prop[4].toInt() * 0.01).toRoundInt()
            if (prop[5].toInt() != 0)
                speed += (speed * prop[5].toInt() * 0.01).toRoundInt()
        }

        //活力特性
        if (isTactics) {
            pa = (pa * 1.5).toRoundInt()
        }

        //契约
        if (contract > 0) {
            hp += contract
            pa += contract
            pd += contract
            sa += contract
            sd += contract
            speed += contract
        }
        //皮肤
        if (isSkin) {
            val split = getSkinValue()
            hp += split[0].toInt()
            pa += split[1].toInt()
            pd += split[2].toInt()
            sa += split[3].toInt()
            sd += split[4].toInt()
            speed += split[5].toInt()
        }
        //亲密度
        if (isIntimacy) {
            hp += 255
            pa += 255
            pd += 255
            sa += 255
            sd += 255
            speed += 100
        }
        if (effort > 0) {
            //判断道具是否是健身环
            val isFitnessRing = (isProp && addition.split(",").size == 12)
            val props = addition.split(",")
            //努力值
            if (effort and HP == HP) {
                hp += EFFORT
                if (isFitnessRing && props[6] == "-1") {
                    hp += (EFFORT / 2)
                }
            }
            if (effort and PA == PA) {
                pa += EFFORT
                if (isFitnessRing && props[7] == "-1") {
                    pa += (EFFORT / 2)
                }
            }
            if (effort and PD == PD) {
                pd += EFFORT
                if (isFitnessRing && props[8] == "-1") {
                    pd += (EFFORT / 2)
                }
            }
            if (effort and SA == SA) {
                sa += EFFORT
                if (isFitnessRing && props[9] == "-1") {
                    sa += (EFFORT / 2)
                }
            }
            if (effort and SD == SD) {
                sd += EFFORT
                if (isFitnessRing && props[10] == "-1") {
                    sd += (EFFORT / 2)
                }
            }
            if (effort and SPEED == SPEED) {
                speed += EFFORT
            }
        }

        //活力特性
        if (isHustle) {
            pa = (pa * 1.5).toRoundInt()
        }

        if (isBig) {
            hp *= 2
        }

    }


    /**
     * 活力特性
     */
    fun isHustle(isHustle: Boolean) {
        this.isHustle = isHustle
        updatePanel()
    }
    /**
     * 达摩狒狒专属特性
     */
    fun isTactics(isTactics: Boolean) {
        this.isTactics = isTactics
        updatePanel()
    }
    /**
     * 保存基础数据
     */
    fun finisInit() {
        baseHp = hp
        basePa = pa
        basePd = pd
        baseSa = sa
        baseSd = sd
        baseSpeed = speed
    }
}